// 进行打包 monorepo
const fs = require("fs");
const { execa } = require('execa');

//1.获取打包文件目录

// 只有文件目录才需要打包
const dirs = fs.readdirSync("packages").filter(p => {
    return fs.statSync(`packages/${p}`).isDirectory()
})
// console.log(dirs) //[ 'reactivity', 'shared' ]


//2.并行打包 
async function build(target) {
    //console.log(target) 
    //execa 返回一个promise 
    //-c执行rollup配置，--environment环境变量    stdio:inherit 子进程的输出在父包中输出
    await execa('rollup', ['-c', '--environment', `TARGET:${target}`],{stdio:"inherit"})
}

//pomise
async function runParaller(dirs, itemfn) {
    //遍历
    let result = []
    for (let item of dirs) {
        result.push(itemfn(item))
    }
    return Promise.all(result) //存放打包pormise
}


runParaller(dirs, build).then(() => {
    console.log("成功 ")
})